#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\cashflows\cashflows.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Cashflows {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICashFlows
	public ref class CCashFlows  : 
            public Cephei::QL::Cashflows::ICashFlows
	{
	protected: 
		boost::shared_ptr<QuantLib::CashFlows>* _ppCashFlows;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::CashFlows>* _phCashFlows;
#endif
		Object^ _CashFlowsOwner;     // reference to object that manages the storage for this object
	internal:
        CCashFlows (boost::shared_ptr<QuantLib::CashFlows>& childNative, Object^ owner);
        CCashFlows (QuantLib::CashFlows& childNative, Object^ owner);
        CCashFlows (CCashFlows^ copy);
        CCashFlows (PLATFORM::Type^ t);
#ifdef STRUCT
        CCashFlows (QuantLib::CashFlows childNative);
#endif       
#ifdef HANDLE
		CCashFlows (QuantLib::Handle<QuantLib::CashFlows>& childNative, Object^ owner);
		CCashFlows (QuantLib::Handle<QuantLib::CashFlows> childNative);
#endif
		virtual ~CCashFlows ();
		!CCashFlows ();

	internal:
		QuantLib::CashFlows& GetReference ();
		boost::shared_ptr<QuantLib::CashFlows>& GetShared ();
		QuantLib::CashFlows* GetPointer ();
        void SetCashFlows (boost::shared_ptr<QuantLib::CashFlows> native)
        {
            if (_ppCashFlows != NULL)
                delete _ppCashFlows;
            _ppCashFlows = new boost::shared_ptr<QuantLib::CashFlows> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::CashFlows>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Double AccruedAmount (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double AtmRate (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate, Microsoft::FSharp::Core::FSharpOption<Double>^ npv) ;
		virtual Double BasisPointValue (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double BasisPointValue (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::IInterestRate^ yield, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double Bps (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double Bps (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::IInterestRate^ yield, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double Bps (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual DateTime MaturityDate (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg) ;
		virtual DateTime StartDate (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg) ;
		virtual Double Convexity (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double Convexity (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::IInterestRate^ yield, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double Duration (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, QL::Cashflows::Duration::TypeEnum type, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double Duration (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::IInterestRate^ yield, QL::Cashflows::Duration::TypeEnum type, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Boolean IsExpired (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double NextCashFlowAmount (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual DateTime NextCashFlowDate (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double NextCouponRate (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double Npv (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::Termstructures::IYieldTermStructure^ discount, Double zSpread, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double Npv (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double Npv (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::IInterestRate^ yield, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double Npv (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double PreviousCashFlowAmount (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual DateTime PreviousCashFlowDate (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double PreviousCouponRate (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double Yield (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Double npv, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxIterations, Microsoft::FSharp::Core::FSharpOption<Double>^ guess) ;
		virtual Double YieldValueBasisPoint (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Double yield, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double YieldValueBasisPoint (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::IInterestRate^ yield, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate) ;
		virtual Double ZSpread (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::Termstructures::IYieldTermStructure^ d, Double npv, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxIterations, Microsoft::FSharp::Core::FSharpOption<Double>^ guess) ;
		virtual Int64 AccrualDays (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual DateTime AccrualEndDate (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double AccrualPeriod (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual DateTime AccrualStartDate (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlDate) ;
		virtual Int64 AccruedDays (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double AccruedPeriod (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate) ;
		virtual Double Nominal (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlDate) ;
		virtual Cephei::QL::Cashflows::ICashFlows^ Npvbps (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Boolean includeSettlementDateFlows, DateTime settlementDate, DateTime npvDate, Double npv, Double bps) ;
		virtual DateTime ReferencePeriodEnd (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlDate) ;
		virtual DateTime ReferencePeriodStart (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlDate) ;
		virtual Double ZSpread (Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ leg, Double npv, Cephei::QL::Termstructures::IYieldTermStructure^ prm1, Cephei::QL::Times::IDayCounter^ dayCounter, QL::CompoundingEnum compounding, QL::Times::FrequencyEnum frequency, Boolean includeSettlementDateFlows, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settlementDate, Microsoft::FSharp::Core::FSharpOption<DateTime>^ npvDate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxIterations, Microsoft::FSharp::Core::FSharpOption<Double>^ guess) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::Cashflows::ICashFlows^>::typeid)]
	[FactoryFor(Cephei::QL::Cashflows::ICashFlows::typeid)]
	[FactoryFor(Cephei::QL::Cashflows::ICashFlows_Factory::typeid)]
	public ref class CCashFlows_Factory sealed : public ICashFlows_Factory
	{
	public:
    };
   
/*Cephei*/ } /*QL*/ } /*Cashflows */}
